<?php
session_start();
$day = null;
$month = null;
$year = null;
if(isset($_REQUEST['day'])){
	$day = $_REQUEST['day'];
}
if(isset($_REQUEST['month'])){
	$month = $_REQUEST['month'];
}
if(isset($_REQUEST['year'])){
	$year = $_REQUEST['year'];
}

$title="แสดงรายการจองทั้งหมด";
include ('header-admin.php');

$page = (isset($_GET['page']))? intval($_GET['page']) : 1;
$limit_end = 30;
$limit_start = ($page-1)*$limit_end;

function page_navi($page, $limit, $range, $count, $send=NULL, $current_style="navi_on", $other_style="navi_out", $target="_self"){
 $output = "";
    $total = ceil($count/$limit);
    $navi_start = $page-$range;
    $navi_end = $page+$range;
     
    $send .= (!empty($send))? "&" : NULL;
         
    if($navi_start <= 0) $navi_start = 1;
    if($navi_end >= $total) $navi_end = $total;

	//$day = $_POST['day'];
	//$month = $_POST['month'];
	//$year = $_POST['year'];
	$day = null;
	$month =  null;
	$year = null;
	
	if(isset($_GET['day']) || isset($_GET['month']) ||  isset($_GET['year'])){
		$day = $_GET['day'];
		$month = $_GET['month'];
	 	$year = $_GET['year'];	
	}

     
    if($page>1){
        $navi_back = $page-1;
        if($page > 2)
        $output .= "<a href=\"?" . $send . "day=".$day."&month=".$month."&year=".$year."&page=1\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&laquo;</strong></a> ";
        $output .= "<a href=\"?" . $send . "day=".$day."&month=".$month."&year=".$year."&page=" . $navi_back . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&#8249;</strong></a> ";
    }
    for($i = $navi_start; $i <= $navi_end; $i++){
        if($i == $page)
        $output .= "<a href=\"?" . $send . "day=".$day."&month=".$month."&year=".$year."&page=" . $i . "\" target=\"" . $target . "\" class=\"" . $current_style . "\"><strong><font color=\"" . $other_style . "\">$i</font></strong></a> ";
        else
        $output .= "<a href=\"?" . $send . "day=".$day."&month=".$month."&year=".$year."&page=" . $i . "\" target=\"" . $target . "\" class=\"" . $other_style . "\">$i</a> ";
    }
    if($page < $total){
        $navi_next = $page+1;
        $output .= "<a href=\"?" . $send . "day=".$day."&month=".$month."&year=".$year."&page=" . $navi_next . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&#8250;</strong></a> ";
        if(($page+1) < $total)
        $output .= "<a href=\"?" . $send . "day=".$day."&month=".$month."&year=".$year."&page=" . $total . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&raquo;</strong></a>";
    }
    if($navi_start>$navi_end)
        $output .= "<a href=\"?" . $send . "day=".$day."&month=".$month."&year=".$year."&page=" . $page . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>$page</strong></a> ";
    return $output;
}


$menu = array('Check In','Check Out');

$userID=$_SESSION["HBD_USER"];
$countTopic=0;
if($userID == 1){
?>
<script language="javascript">
	function ConfirmChoice(id){
	answer = confirm("คุณแน่ใจว่าจะลบ ?")
		if (answer !=0)
			location = "doDeleteNews.php?newsID=" + id;
	}
</script>
<?}?>
<div id="center-block" class="main-content">
<img src="img/report_byhouse.png"/ id="img-block-content">
<div id="content-title-right">
		<h2 class="caption-header">แสดงรายการเข้าพัก</h2>
		<span class="caption">แสดงประวัติการ Check Inและ Check Out</span>
		<span class="right-button">
		   <form name="jump" method="POST" action="listLog.php?page=1">
		   	<select name="day" id="day" style="width: 50px">
		      <?
				echo '<option value="">วัน</option>';
				for($i=1;$i<=31;$i++){
					if($day == $i)
						echo '<option value="' . $i . '" SELECTED>' . $i . '</option>';
					else
						echo '<option value="' . $i . '">' . $i . '</option>';
				}
				
		      ?>
			</select>
			<select name="month" id="month" style="width: 70px">
		      <?
				$monthname = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
				echo '<option value="">เดือน</option>';
				$i=1;
				foreach($monthname as $month_text){
					if($month == $i)
						echo '<option value="' . $i . '" SELECTED>' . $month_text . '</option>';
					else
						echo '<option value="' . $i . '">' . $month_text . '</option>';
					$i++;
				}
		      ?>
			</select>
			<select name="year" id="year" style="width: 60px">
		      <?
				for($i=2011;$i<=2050;$i++){
					if($year == $i)
						echo '<option value="' . $i . '" SELECTED>' . $i . '</option>';
					else
						echo '<option value="' . $i . '">' . $i . '</option>';
				}
					
		      ?>
			</select>
			&nbsp;<input type="submit" value="ค้นหา" class="button">
		   </form>
				</span>
	</div>
<script language="javascript">
	function printReportPage(){
		window.open('print.php?ref=p_checkinout&day=<?php echo $day;?>&month=<?php echo $month;?>&year=<?php echo $year;?>', 'printpage', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=800,height=480')
                      return false;
	}
</script>
<div style="margin-top:-35px;" class="center"><a href="#" onclick="printReportPage();"><img src="./img/printer.png" alt="print" width="32px" height="32px"><span style="padding:5px 0 0 5px;">กดปุ่มเพื่อทำการพิมพ์ข้อมูลผ่านเครื่องพิมพ์</span></a>
	</div>
	<hr>
	<table summary="webboard about Datastrucutre learning" id="table">
		<thead>
		<tr class="odd">
<!-- 			<th scope="col" abbr="reserv_id">ID</th> -->
<!-- 			<th scope="col" abbr="reserv_id">เลขที่ใบจอง</th> -->
			<th scope="col" abbr="reserv_begin">วันที่เข้าพัก</th>	
<!-- 			<th scope="col" abbr="reserv_begin">เวลาจองออก</th> -->
			<th scope="col" abbr="reserv_begin">เวลาเช็คอิน</th>	
			<th scope="col" abbr="reserv_begin">เวลาเช็คเอาท์</th>	
			<th scope="col" abbr="reserv_end">ชื่อลูกค้า</th>
			<th scope="col" abbr="room_id">เลขห้อง</th>
			<th scope="col" abbr="customer_id">ชนิดห้อง</th>
		</tr>	
		</thead>
		<tbody>
		<?
		$begin_day = $day;
		$end_day = $day;
		$begin_month = $month;
		$end_month = $month;


		if($day == "" || !isset($day)){
			$begin_day = 1;
			$end_day = 31;
		}
		if($month == "" || !isset($month)){
			$begin_month = 1;
			$end_month = 12;
		}

		$beginquery = $year . "-" . $begin_month . "-" . $begin_day;
		$endquery = $year . "-" . $end_month . "-" . $end_day;

		if($day== "" && $month == "" && $year == "")
		{$datestring = "";}
		else
		{$datestring = "WHERE reserv_begin between '$beginquery' and '$endquery'";}
		
		$sql = "SELECT reserv_begin,reserv_end,checkin_time,checkout_time,reservation.room_id,customer_name,customer_surname,roomtype_name
			FROM reservation
			INNER JOIN resort ON (reservation.room_id = resort.room_id)
			INNER JOIN roomtype ON (resort.roomtype_id	= roomtype.roomtype_id)
			INNER JOIN customer ON (reservation.id = customer.id)
			INNER JOIN checkin ON (reservation.reserv_id = checkin.reserv_id)
			INNER JOIN checkout ON (reservation.reserv_id = checkout.reserv_id)
			" . $datestring . " ORDER BY reserv_begin ASC";
			
		if($day != ""){
			$choosetime = $year."-".$begin_month."-".$begin_day;
			$sql="SELECT reserv_begin,reserv_end,checkin_time,checkout_time,reservation.room_id,customer_name,customer_surname,roomtype_name
			FROM reservation
			INNER JOIN resort ON (reservation.room_id = resort.room_id)
			INNER JOIN roomtype ON (resort.roomtype_id	= roomtype.roomtype_id)
			INNER JOIN customer ON (reservation.id = customer.id)
			INNER JOIN checkin ON (reservation.reserv_id = checkin.reserv_id)
			INNER JOIN checkout ON (reservation.reserv_id = checkout.reserv_id)
   WHERE '$choosetime' BETWEEN reserv_begin AND reserv_end ORDER BY reserv_begin ASC";
   }


	$result = mysql_query($sql. " LIMIT " . $limit_start. "," . $limit_end);
	$count = mysql_num_rows($result);
	$total = mysql_num_rows(mysql_query($sql));
if($total > 0){
	while ($rs = mysql_fetch_array($result)){
		$countTopic++;
		if($countTopic%2==0)
			echo '<tr >';
		else
			echo '<tr >';
/* 		if($type=="1")echo '<td>' . $rs[checkin_id] . '</a>'; else echo '<td>' . $rs[checkout_id] . '</a>'; */
/* 		echo '<td>' . $rs[reserv_id] . '</a>'; */
/* 		$rs[ReplyCount]--; */
/* 		echo '</td>'; */
		echo '<td>' . $rs['reserv_begin'] . ' - '.$rs['reserv_end'].'</td>';
/* 		echo '<td>' . $rs['reserv_end'] . '</td>'; */
		echo '<td>' . $rs['checkin_time'] . '</td>';
		echo '<td>' . $rs['checkout_time'] . '</td>';
		echo '<td>' . $rs['customer_name'] .' '.$rs['customer_surname'].'</td>';
		echo '<td>' . $rs['room_id'] . '</td>';
		echo '<td>' . $rs['roomtype_name'] . '</td>';
	}

echo "\n<div style=\"float:left;\">Page : " . page_navi($page, $limit_end, 4, $total, $send) . " </div><div style=\"float:right;\">[" . ($limit_start+1) . "-" . ($limit_start+$count) . "] of <strong>" . $total . "</strong></div>";
}
	?>
		</tbody>

	</table>
	</div>

<?
include ('footer.php');
?>